﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WeixinMP.DataAccess;
using Microhard.WeixinMP.Models;
using System.Configuration;
using Microhard.WeixinMP.DTO;
using Microhard.WeixinMP.Interfaces;
using System.Web;
using HtmlAgilityPack;
using Microhard.Core.Extensions;


namespace Microhard.WeixinMP.Impls
{
    public class StaticMsgSender : BaseResponseImpl
    {
        public override Interfaces.WeixinCommand Command
        {
            get
            {
                WeixinCommand cmm = new WeixinCommand();
                cmm.Event = "CLICK";
                return cmm;
            }
        }
        public override bool IsCallMe(Microhard.WeixinMP.Models.RequestServiceModel model)
        {
            if (model.Event == null)
                return false;
            if (model.Event.Content == null)
                return false;
            string cmd = model.Event.Content.EventKey;

            if (!string.IsNullOrEmpty(cmd))
            {
                MicrohardWXEntities db = new MicrohardWXEntities();
                var cnt = db.MPInfoes.Count(m => m.ClickKey == cmd);
                return cnt > 0 ? true : false;
            }
            return false;
        }

        protected override Models.WeixinBaseData<object> CreateFeatureResponse(Models.RequestServiceModel request, Models.WeixinBaseData<object> response)
        {
            //创建会话成功，发起服务
            response.MsgType = WeixinMsgType.News;
            response.FuncFlag = WeixinMsgType.FuncFlag_False;
            //获得信息
            MicrohardWXEntities db = new MicrohardWXEntities();

            var msg = db.MPInfoes.SingleOrDefault(i => i.BelongEntityID == request.WXMPAccountUser.CompanyID && i.ClickKey == request.Event.Content.EventKey);
            SendToUserInfoModel model = msg.ToModel();
            string url = ConfigurationManager.AppSettings["StaticMsgUrl"] + "?id=" + msg.InfoID;
            //获得文章图片
            var html = HttpUtility.HtmlDecode(msg.Content.IndentHtml());
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);
            var nodes = doc.DocumentNode.SelectNodes("//img");
            string picUrl = string.Empty;
            if (nodes != null && nodes.Count > 0)
            {
                var r = new Random();
                var c = r.Next(0, nodes.Count - 1);
                picUrl = ConfigurationManager.AppSettings["BaseUrl"] + nodes[c].Attributes[@"src"].Value;
            }

            WeixinNews_Response ask = new WeixinNews_Response();
            ask.Items = new List<WeixinNewsItem_Response>();
            ask.Items.Add(new WeixinNewsItem_Response
            {
                Order = 1,
                Description = model.Summary,
                Title = model.Title,
                PicUrl = picUrl,
                Url = url
            });
            response.Content = ask;
            return response;
        }
    }
}
